package org.example.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.bean.Orderdetail;
import org.example.bean.Orders;
import org.example.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/orders")
@Tag(name = "订单管理",description = "实现对订单的增删改查工作")
public class OrdersController {
    public static final int ORDER_STATUS_WAIT= 0;
    public static final int ORDER_STATUS_ACTIVE= 1;
    public static final int ORDER_STATUS_COMPLETE= 2;
    @Autowired
    private OrdersService ordersService;
    //添加订单
    @PostMapping("/add")
    @Operation(summary = "添加订单",description = "根据用户id和地址id添加订单")
    public ResponseEntity<ResponseResult> addOrder(@RequestParam("user_id") Integer user_id,
                                                   @RequestParam("address_id") Integer address_id){
        Orders orders = new Orders();
        orders.setUser_id(user_id);
        orders.setAddress_id(address_id);
        orders.setOrder_time(new Date());
        orders.setOrder_status(ORDER_STATUS_WAIT);
        ordersService.addOrder(orders);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("添加成功").data(orders).build());
    }

    //多表查询未完成的订单
    @GetMapping("/findUnfinishedOrder/{order_status1}")
    @Operation(summary = "多表查询未完成的订单",description = "根据订单状态查询未完成的订单")
    public ResponseEntity<ResponseResult> findUnfinishedOrder(@PathVariable("order_status1") Integer order_status1){
        List<Orders> orderdetailList = ordersService.findUnfinishedOrder(order_status1);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(orderdetailList).build());
    }

    //根据订单id修改订单状态
    @PutMapping("/updateOrderStatus/{order_id}/{order_status}")
    @Operation(summary = "根据订单id修改订单状态",description = "根据订单id修改订单状态")
    public ResponseEntity<ResponseResult> updateOrderStatus(@PathVariable("order_id") Integer order_id,
                                                             @PathVariable("order_status") Integer order_status){
        boolean result = ordersService.updateOrderStatus(order_id, order_status);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("修改成功").data(result).build());
    }

    //根据订单id查询订单详情
    @GetMapping("/findOrderById/{order_id}")
    @Operation(summary = "根据订单id查询订单详情",description = "根据订单id查询订单详情")
    public ResponseEntity<ResponseResult> findOrderById(@PathVariable("order_id") Integer order_id){
        List<Orders> orders = ordersService.findOrderById(order_id);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(orders).build());
    }


    @PostMapping("{order_id}")
    public void order(@PathVariable("order_id") Integer order_id){
        ordersService.order(order_id);
    }
}
